import axios from "axios";
import { MessageBox } from "element-ui";
import { operationCtx } from "./auth";
import store from "../store";

// 上海环境：生产
window.fileLoadUrl = process.env.NODE_ENV === "development" ? "http://10.135.56.115:8559/dz_bjdq-service/bjdq/offer/saveImageFile" : "http://10.135.56.115:8559/dz_bjdq-service/bjdq/offer/saveImageFile";

const {VUE_APP_ENV} = process.env;
let baseUrl = "http://10.135.56.101:9011/backend/";  // 测试环境
if (VUE_APP_ENV == 'prod') {
	baseUrl = "http://10.135.139.145:8559/backend/";
}
// 南京环境：正式
window.baseUrl = process.env.NODE_ENV === "development" ? baseUrl : baseUrl;
// window.baseUrl = "http://10.135.139.145:8559/backend/";


let http = axios.create({
	baseURL: window.baseUrl,
	timeout: 60000 * 5
})
http.interceptors.request.use(config => {
	console.log(config,"config");
	let data = config.data;
	config.data = {
		req: data,
		operationCtx
	}
	config.cancelToken = new axios.CancelToken(e => {
		store.commit('addCancelToken', {
			cancel: e,
			url: location.host + config.url
		})
	})
	return config;
}, err => {
	return Promise.reject(err);
});

http.interceptors.response.use((result) => {
	console.log(result, "result");
	let {data} = result;
	if (typeof data == 'string') {
		return result;
	} else {
		let {success} = data;
		if (!success) {
			let { errorCode, errorMessage } = data;
			MessageBox.alert(errorMessage, "错误码：" + errorCode, {
				type: "error",
				confirmButtonText: "确定"
			});
			return null;
		} else {
			return data;
		}
	}
}, err => {
	// console.log(err, "err");
	if (http.isCancel(err)) {
		console.warn(err);
	}
	let errMess = "", errCode = "";
	if (err.code === 'ECONNABORTED' && err.message.indexOf('timeout') !== -1) {
		errCode = "timeout";
		errMess = "请求超时，后台服务连接失败";
	}
	if (!err.code && err.message.indexOf('Network Error') !== -1) {
		errCode = "Network Error";
		errMess = "网络异常，请检查网络或vpn；如果有问题请联系管理员";
	}
	if (err.response) {
		if (err.response.status === 504) {
			errCode = 504;
			errMess = "服务器走丢了，网关超时";
		} else if (err.response.status === 500) {
			errCode = 500;
			errMess = "服务器内部服务错误";
		} else if (err.response.status === 400) {
			errCode = 400;
			errMess = "请求参数错误";
		} else if (err.response.status === 401) {
			errCode = 401;
			errMess = "登录信息失效";
		} else if (err.response.status === 404) {
			errCode = 404;
			errMess = "资源或接口丢失";
		}
	}
	MessageBox.alert(errMess, "错误码：" + errCode, {
	    type: "error",
	    confirmButtonText: "确定"
	});
	return Promise.reject(err);
});

export default http;
export const httpBehaviorUrl = process.env.VUE_APP_ENV === 'prod' ? 'http://10.135.139.145:8559' : 'http://10.135.56.101:9011';
export const httpComputedUrl = process.env.VUE_APP_ENV === 'prod' ? 'http://10.135.139.145:8559' : 'http://10.135.56.101:9021';

// 测试数据
// export const httpBehaviorUrl = process.env.VUE_APP_ENV === 'prod' ? 'http://10.135.139.145:8559' : 'http://10.135.139.145:8559';
// export const httpComputedUrl = process.env.VUE_APP_ENV === 'prod' ? 'http://10.135.139.145:8559' : 'http://10.135.139.145:8559';
